//
//  BizApplication.m
//  WineCellar
//
//  Created by CoolCamel on 2017/11/21.
//  Copyright © 2017年 www.biz-united.com.cn. All rights reserved.
//

#import "BizApplication.h"
@interface BizApplication() <UINavigationControllerDelegate>
@property (nonatomic, weak) id<UIApplicationDelegate> appDelegate;
@property (nonatomic, copy) BizPayResultBlock payResultBlock;
@property (nonatomic, copy) BizWechatAuthBlock wechatAuthBlock;
@property (nonatomic, copy) BizWechatShareBlock wechatShareBlock;
@property (nonatomic, copy) BizQQShareBlock qqShareBlock;
@property (nonatomic, strong) NSNumber * isNoNetwork;

@end

@implementation BizApplication {
    NSUInteger * _timerCounter;
    NSTimer * _timer;
}
+ (instancetype)shareInstance {
    static BizApplication * _instance = nil;
    static dispatch_once_t _onceToken;
    
    dispatch_once(&_onceToken, ^{
        
        _instance = [[BizApplication alloc] initWithAppDelegate:[UIApplication sharedApplication].delegate];
    });
    
    return _instance;
}

- (instancetype)initWithAppDelegate:(id<UIApplicationDelegate>)appDelegate {
    if(self = [super init]) {
        __weak typeof(id<UIApplicationDelegate>) weakAppDelegate = appDelegate;
        self.appDelegate = weakAppDelegate;
        
        _timerCounter = 0;
    }
    
    return self;
}

- (void)configWithOptions:(NSDictionary *)launchOptions {
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 10.0f;
}


- (void)configRootWindowWithOptions:(NSDictionary *)launchOptions {
    typedef void (^Animation)(void);
    
    if(nil == self.appDelegate)
        return;
    
    UIImage * navigationBKImage = [UIImage imageWithColor:COLOR_WINDOW_THEME
                                                     size:CGSizeMake(iPhone_Width,
                                                                     iPhone_NavigationBar_Height)];
    [[UINavigationBar appearance] setBackgroundImage:navigationBKImage
                                       forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:
                                                               UIColorFromRGB(0xFFFFFF)}];
    [[UINavigationBar appearance] setTintColor:UIColorFromRGB(0X666666)];
    [[UITabBar appearance] setBarStyle:UIBarStyleDefault];
    [[UITabBar appearance] setTranslucent:NO];
    self.appDelegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.appDelegate.window.backgroundColor = COLOR_WHITE;
    
    [self.appDelegate.window makeKeyAndVisible];
    
    if(!self.isNoNetwork.boolValue) {
        if([self.appDelegate.window.rootViewController isKindOfClass:FSRootWindow.class]) {
            return;
        }
        
        FSRootWindow * rootWindow = [[FSRootWindow alloc] init];
        rootWindow.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        Animation animation = ^{
            BOOL oldState = [UIView areAnimationsEnabled];
            [UIView setAnimationsEnabled:NO];
            [[self.appDelegate window] setRootViewController:rootWindow];
            [[self.appDelegate window] makeKeyAndVisible];
            [UIView setAnimationsEnabled:oldState];
        };
        
        [UIView transitionWithView:[self.appDelegate window]
                          duration:0.5f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:animation
                        completion:nil];
        
        
       
    }
}

- (void)configNotificationWithDeviceToken:(NSData *)deviceToken {
    
}


#pragma mark - 定时器
- (void)startTimer {
    _timerCounter ++;
    if(nil == _timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(timerTick)
                                                userInfo:nil
                                                 repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

- (void)cancelTimer {
    _timerCounter --;
    if(0 == _timerCounter && nil != _timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)suspendTimer {
    if(nil != _timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)resumeTimer {
    if(_timerCounter > 0 && nil == _timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(timerTick)
                                                userInfo:nil
                                                 repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

- (void)timerTick {
    [[NSNotificationCenter defaultCenter] postNotificationName:kBiz_Global_Notification_Timer
                                                        object:nil];
}
- (BOOL)isNativeURL:(NSString *)URLString {
    NSArray * components = [URLString componentsSeparatedByString:@"#"];
    if(nil == components || 2 != components.count) {
        return NO;
    }
    
    NSString * replacedURLString = components[0];
    NSString * page = components[1];
    
    //url编码，非编码处理
    NSURL * url = [NSURL URLWithString:replacedURLString];
    if(!url) {
        NSString * encodedURLString = [replacedURLString stringByAddingPercentEncodingWithAllowedCharacters:
                                       [NSCharacterSet URLQueryAllowedCharacterSet]];
        
        if(!encodedURLString) {
            return NO;
        }
        
        url = [NSURL URLWithString:encodedURLString];
    }
    
    //分类
    if([page rangeOfString:@"goodsCategory.html"].location != NSNotFound ||
       [page rangeOfString:@"goodsDetail.html"].location != NSNotFound ||
       [page rangeOfString:@"coupon.html"].location != NSNotFound ||
       [page rangeOfString:@"couponCenter.html"].location != NSNotFound ||
       [page rangeOfString:@"memberWallet.html"].location != NSNotFound ||
       [page rangeOfString:@"memberInformation.html"].location != NSNotFound ||
       [page rangeOfString:@"kill.html"].location != NSNotFound ||
       [page rangeOfString:@"booking.html"].location != NSNotFound ||
       [page rangeOfString:@"depreciate.html"].location != NSNotFound ||
       [page rangeOfString:@"member.html"].location != NSNotFound ||
       [page rangeOfString:@"lettersList.html"].location != NSNotFound) {
        
        return YES;
    }
    
    return NO;
}

@end
